<?php
session_start();
ob_start();
include("../function.php");
condb();
$res_id=$_GET['res_id'];
$search=$_GET['search'];
?>
<meta charset="utf-8" />
<center>
	<form>
    	<input name="search" />
        <input type="hidden" name="res_id" value="<?php echo $res_id;?>" />
        <input type="submit" value="ค้นหา" />
    </form>
</center>
<?php
  #แบ่งหน้า
  $sql="select * from pharmacy where pmc_name like '%$search%' and pmc_amount>0 order by pmc_id";
  $res=mysql_query($sql);
  $page=$_GET['page'];
  if(!$page)$page=1;
  $allrows=mysql_num_rows($res);
  $allpage=ceil($allrows/$cfg_limit);
  $limitstart=($page-1)*$cfg_limit;
  $sql.=" limit $limitstart,$cfg_limit";
  $res=mysql_query($sql);
  $pageurl="getpharmacy.php?search=$search&res_id=$_GET[res_id]";
  #สิ้นสุดการแบ่งหน้า
  if($allrows>0){
?>
<table width="680" cellpadding="0">
  <tr bgcolor="#00ddff">
    <td align="center"><strong>ลำดับที่</strong></td>
    <td><strong>ชื่อยา</strong></td>
    <td><strong>หมวด</strong></td>
    <td width="80" align="center"><strong>คงเหลือ</strong></td>
    <td><strong>หน่วย</strong></td>
    <td align="center"><strong>ดำเนินการ</strong></td>
  </tr>
  <?php
  $i=0;
  while($row=mysql_fetch_assoc($res)){
	  $cat=mysql_fetch_assoc(mysql_query("select * from pharmacycat where cat_id='$row[cat_id]'"));
	   $unit=mysql_fetch_assoc(mysql_query("select * from pharmacyunit where unit_id='$row[unit_id]'"));
	  $i++;
	  $bg=($i%2==0)?'#ccffcc':'ffffff';
	  	$html="$row[pmc_name]<input type=hidden name=pmc_id[] value=$row[pmc_id]>";
  ?>
  <tr bgcolor="<?php echo $bg;?>">
    <td align="center"><?php echo $i;?></td>
    <td><?php echo $row['pmc_name'];?></td>
    <td><?php echo $cat['cat_name'];?></td>
    <td width="80" align="right"><?php echo number_format($row['pmc_amount'],2);?></td>
    <td align="left"><?php echo $unit['unit_name'];?></td>
    <td align="center"><a href="#" onclick="addp('<?php echo $res_id;?>','<?php echo $html;?>',<?php echo $row['pmc_amount']?>);">เพิ่มยานี้</a></td>
  </tr>
  <?php
  }
  ?>
</table>
  <?php
	  pagemenu($pageurl,$page,$allpage);
  }
  ?>
<center><input type="button" value="ปิดหน้าต่างนี้" onclick="window.close();" /></center>
<script>
function addp(res_id,html,amount){
	var num;
	do{
		var next=false;
		num=prompt('กรุณากรอกจำนวน (ไม่เกิน'+amount+')','1');
		if(parseInt(num)>0 && parseInt(num)<=amount){
			next=confirm('เพิ่มยานี้จำนวน '+num +' ยืนยัน?');
		}
	}while(parseInt(num)<0 || next==false);
	var res=window.opener.document.getElementById(res_id).innerHTML;
	var res = res + html +'('+ num +')<input type=hidden name=amount[] value='+ num +'><br>';
	window.opener.document.getElementById(res_id).innerHTML=res;
}
</script>